﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace UtilZ.Dotnet.Ex.Json
{
    /// <summary>
    /// 
    /// </summary>
    public class JsonNodeCollection : ICollection<JsonNode>
    {
        private List<JsonNode> _list = new List<JsonNode>();
        private readonly Dictionary<string, JsonNode> _dic = new Dictionary<string, JsonNode>();

        /// <summary>
        /// 
        /// </summary>
        public JsonNodeCollection()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        public int Count
        {
            get { return _list.Count; }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool IsReadOnly
        {
            get { return false; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public void Add(JsonNode item)
        {
            if (!string.IsNullOrEmpty(item.JsonMemberName))
            {
                this._dic.Add(item.JsonMemberName, item);
            }

            this._list.Add(item);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="collection"></param>
        public void AddRange(IEnumerable<JsonNode> collection)
        {
            if (collection == null)
            {
                return;
            }

            foreach (var item in collection)
            {
                if (!string.IsNullOrEmpty(item.JsonMemberName))
                {
                    this._dic.Add(item.JsonMemberName, item);
                }
            }
            this._list.AddRange(collection);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Remove(JsonNode item)
        {
            if (!string.IsNullOrEmpty(item.JsonMemberName))
            {
                this._dic.Remove(item.JsonMemberName);
            }

            return this._list.Remove(item);
        }

        /// <summary>
        /// 
        /// </summary>
        public void Clear()
        {
            this._dic.Clear();
            this._list.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        public void Sort()
        {
            if (this._list.Count == 0)
            {
                return;
            }

            this._list = this._list.OrderBy(t => { return t.Order; }).ToList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="jsonMemberName"></param>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool TryGetValue(string jsonMemberName, out JsonNode node)
        {
            return this._dic.TryGetValue(jsonMemberName, out node);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(JsonNode item)
        {
            return this._list.Contains(item);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        public void CopyTo(JsonNode[] array, int arrayIndex)
        {
            this._list.CopyTo(array, arrayIndex);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<JsonNode> GetEnumerator()
        {
            return this._list.GetEnumerator();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
}
